Accès aux éléments HTML depuis Silverlight
Hello
.....
HtmlElement elem = doc.GetElementByID("zaMsg");
elem.SetAttribute("innerHTML", "Nouveau");
elem.SetStyleAttribute("background", "green");
elem.SetStyleAttribute("color", "white");
.....
HtmlElement elem = doc.GetElementByID("zeNom");
string s = elem.GetAttribute("value"); // contenu de la zone d’édition dans s
elem.SetAttribute("value", "Votre nom ici"); // modification du contenu
.....
HtmlElement elem = doc.GetElementByID("img");
elem.SetAttribute("src", "Vous.jpg");
.....
HtmlElement elem = doc.GetElementByID("villes");
string s = elem.GetAttribute("selectedIndex");
int n = Int32.Parse(s);
.....
HtmlElement elem = doc.GetElementByID("bGo");
elem.SetAttribute("value", "En avant !");
HtmlDocument doc = HtmlPage.Document;
HtmlElement table = doc.CreateElement("table");
table.SetAttribute("width", "100%");
table.SetStyleAttribute("background-color", "Beige");
table.SetAttribute("border", "1");
// création de la première rangée
HtmlElement li0 = doc.CreateElement("tr");
HtmlElement colPays = doc.CreateElement("td"); // création de la première cellule
colPays.SetAttribute("innerText", "Pays"); // initialisation de son contenu
li0.AppendChild(colPays); // accrocher la cellule colPays à la ligne li0
HtmlElement colPop = doc.CreateElement("td"); // création de la deuxième cellule
colPop.SetAttribute("innerText", "Population"); li0.AppendChild(colPop);
table.AppendChild(li0); // accrocher la ligne
// création de la deuxième rangée
HtmlElement li1 = doc.CreateElement("tr");
HtmlElement colFrance = doc.CreateElement("td"); // création de la première cellule
colFrance.SetAttribute("innerText", "France"); li1.AppendChild(colFrance);
HtmlElement colFrancePop = doc.CreateElement("td"); // création de la deuxième cellule
colFrancePop.SetAttribute("innerText", 64.5.ToString());
li1.AppendChild(colFrancePop);
table.AppendChild(li1);
// création de la troisième rangée
HtmlElement li2 = doc.CreateElement("tr");
HtmlElement colItalie = doc.CreateElement("td");
colItalie.SetAttribute("innerText", "Italie"); li2.AppendChild(colItalie);
HtmlElement colItaliePop = doc.CreateElement("td");
colItaliePop.SetAttribute("innerText", 59.2.ToString());
li2.AppendChild(colItaliePop);
table.AppendChild(li2);
// accroch la table dans la cellule cellR1C0
doc.GetElementById("cellR1C0").SetAttribute("innerHTML",
table.GetAttribute("outerHTML"));
Attacher des événements
.....
HtmlElement elem = doc.GetElementByID("bGo");
elem.AttachEvent("onclick", new EventHandler(bGo_onClick));
private void bGo_onClick(object sender, HtmlEventArgs e)
{
.....
}
.....
HtmlElement elem = doc.GetElementByID("villes");
elem.AttachEvent("onchange",
new EventHandler(villes_onChange));
.....
private void villes_onChange(object sender, HtmlEventArgs e)
{
HtmlElement elem = e.SourceElement;
sSel = elem.GetAttribute("value");
}
Appeler une fonction JavaScript
ScriptObject jsF = (ScriptObject)HtmlPage.Window.GetProperty("f");
jsF.InvokeSelf("Blabla");
Appeler une fonction C# à partir de JavaScript
using System.Windows.Browser;
.....
[ScriptableType]
public class Calcul
{
[ScriptableMember]
public int Somme(int a, int b) { return a + b; }
}
}
using System.Windows.Browser;
.....
private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
{
Calcul calc = new Calcul();
HtmlPage.RegisterScriptableObject("CalculSL2", calc);
}
N1 :
N2 :
Animation Flash dans une page Silverlight
.....
.....
.....
private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
{
LayoutRoot.SizeChanged += new SizeChangedEventHandler(LayoutRoot_SizeChanged);
}
GeneralTransform gt = can.TransformToVisual(LayoutRoot);
Point offset = gt.Transform(new Point(0, 0));
int X = (int)offset.X;
int Y = (int)offset.Y;
HtmlDocument doc = HtmlPage.Document;
HtmlElement elem = doc.GetElementById("flash");
elem.SetStyleAttribute("left", X + "px");
elem.SetStyleAttribute("top", Y + "px");